
import getopt, sys

def main():
    
    try:
        opts, args = getopt.getopt(sys.argv[1:], "hi:o:v")
    except getopt.GetoptError:
        # print help information and exit:
        usage()
        sys.exit(2)
        
    inputFile = ''
    outputFile = ''
    
    for o, a in opts:
        if o in ("-h", "--help"):
            usage()
            sys.exit()
        if o in ("-i", "--input"):
            inputFile = a            
        if o in ("-o", "--output"):
            outputFile = a            
        if o == "-v":
            usage()
            sys.exit()
        if o == "--version":
            showVersion()
            sys.exit()
    # ...
    
#    print "----inputFile=" + inputFile
#    print "----outputFile=" + outputFile
    
    for o in args:
        print "----args=" + o
        
    startID = 0
    endID = 0

    str_pagePath = inputFile
    str_outputPath = inputFile

    htmlKey = 'href="http://www.cnqsq.com/zhuanti'
    keyLen = len(htmlKey)
    
    str_NextPageKey = "<a href='Next.htm'"
    
    if str_pagePath.find('.htm')<0:
        str_pagePath = str_pagePath + "index.htm"
        print str_pagePath
    all_the_text = readFile(str_pagePath)
    
    s = all_the_text
    s = s.replace('href="','\nhref="')
    prior_id = ''
    
    IDlist = [0]
    htmlFullBody = ''
    ssplit = s.split('\n')
    
    vlist =["::::"]

    for tmpLine in ssplit :
        if len(tmpLine)> keyLen :
#            print "ORG=" + tmpLine
            if not cmp(tmpLine[0:keyLen],htmlKey[0:keyLen]) :
                #print tmpLine
                tmp = tmpLine.split('"')
                tmpCmd = 'call getpage.cmd "' + tmp[1] + '"'
                if not tmpCmd in vlist:
                    vlist.append(tmpCmd)
#                nPos = tmpLine.index('.htm">')
#                #print nPos
#                str_fileID = tmpLine[keyLen:nPos]
#                #print '========== Start process ... ' + str_fileID
#                IDlist.append(str_fileID)
#                if prior_id:
#                    # process
#                    str_tmpPageFile = str_pagePath + prior_id + ".htm"
#                    #print 'prior page( str_tmpPageFile ) = ' + str_tmpPageFile
#                    str_Page = readFile(str_tmpPageFile)                    
#                    if (not str_Page) or (len(str_Page)<1):
#                        print "None .1"
#                        sys.exit()
#                    if len(str_Page) > 0 :
#                        #print "---len=" + str(len(str_Page))
#                        tp = str_Page
#                        tp = tp.replace(str_NextPageKey,"<a href='" + str_fileID + ".htm'")
#                        print '[' + prior_id + '.htm' + "].Next Page" + ' ==> ' + str_fileID + '.htm'
#                        writeFile(str_outputPath + prior_id + ".htm", tp)
                #else:
                    #print "null"
#                prior_id = str_fileID
    #print IDlist
    page_id = 0
    for tmpLine in vlist :
        print tmpLine + " " + str(page_id) + ".htm"
        page_id = page_id + 1


    
def usage():
    print "----in usage"

def readFile(fileName):
    all_the_text = None
    file_object = open(fileName)
    try:
        all_the_text = file_object.read( )
    except IOError, message:    # file open failed
        #print >> sys.stderr, "File could not be opened:", message
        print "**333" + "File could not be opened:", message
        return None
    finally:
        file_object.close( )
#    if all_the_text:
        return all_the_text
#    else:
#        print "**222"
#        return ''
        
def writeFile(fileName, fileBody):
    # open file
    try:
        file = open(fileName, "w" ) # open file in write mode
    except IOError, message:    # file open failed
        print >> sys.stderr, "File could not be opened:", message
        sys.exit( 1 )
    print >> file, fileBody        # write entry to file
    file.close()

if __name__ == "__main__":
    main()
    